<?php


function ez_set_cache($name,$arr,$type='json'){
	switch($type){
		case 'json':
			$data=PHP_EXIT.json_encode($arr);
			ez_file_put(EZROOT .'/data/json/' . $name . '.php', $data);
		break;
		case 'array':
			$data = "<?php\n return " . var_export($arr, true) . ";\n?>";
			ez_file_put(EZROOT .'/data/array/' . $name . '.php', $data);
		break;
	}
}


function ez_del_cache($name,$type='json'){
	switch($type){
		case 'json':
			@unlink(EZROOT .'/data/json/' . $name . '.php');
		break;
		case 'array':
		    @unlink(EZROOT .'/data/array/' . $name . '.php');
		break;
	}
}


function ez_get_cache($name,$type='json'){
	switch($type){
		case 'json':
			$data=array();
			if(is_file(EZROOT .'/data/json/' . $name . '.php')){
				$data=file_get_contents(EZROOT .'/data/json/' . $name . '.php');
				$data=preg_replace('/^'.PHP_EXIT_PREG.'/','',$data);
				$data=json_decode($data,1);
               // var_dump($data);
				if(empty($data)){$data=array();}
			}
		break;
		case 'array':
			$data=array();
			if(is_file(EZROOT .'/data/array/' . $name . '.php')){
				$data = include(EZROOT .'/data/array/' . $name . '.php');
				if(empty($data)){$data=array();}
			}
		break;
	}
	return $data;
}


function jumpajax($code,$msg,$url="",$callbackType="",$navTabId=""){
    global $ezhand,$id;
    $arr["statusCode"]=$code;
    $arr["message"]=$msg;
    $arr["navTabId"]=$navTabId;
    $arr["rel"]="";
    $arr["callbackType"]=$callbackType;
    $arr["forwardUrl"]=$url;
    $arr["confirmMsg"]="";
	echo json_encode($arr);

 	if(ACT=='set'){
      	    $ezhand->admin_log('set');
   	}elseif(ACT=='addedi'){
        if($id>0){
        		    $ezhand->admin_log('update');
        }else{
        		    $ezhand->admin_log('insert');
      	}
   	}elseif(ACT=='del'){
        	$ezhand->admin_log('delete');
    }
    
	ez_exit();   
}

function jump_error($word='') {

    $alert="alert('" . $word . "');";
     echo script($alert);
     
     ez_exit();

}
function jump($url = '',$word='') {
	if(defined('AJAX') && AJAX==1) {
		if($word!=''){
		    $arr=array('s'=>0,'id'=>$word);
		}
		else{
		    $arr=array('s'=>1);
		}
		echo json_encode($arr);
		ez_exit();
    }
    else{
	    if($word!=''){
		    if(is_numeric($word)){
				global $errorData;
			    $alert="alert('" . $errorData[$word] . "');";
			}
			else{
			    $alert="alert('" . $word . "');";
			}
		}
	    else {
			$alert='';
		}
        if($url==-1){
        	$url=$_SERVER["HTTP_REFERER"];
        }
	    if (is_numeric($url)) {
		    echo script($alert.'history.go('.$url.');');
	    } else {
            echo script($alert.'window.location.href="' . $url . '";');
			//echo '<meta http-equiv="Refresh" content="0; url='.$url.'" />';
	    }
	    ez_exit();
	}
}
function highliang($keyword,$subject){
    $model = preg_replace("&($keyword)&iU","<span style='color:rgb(255,0,0);'>\\1</span>",$subject); 
    return $model; 
}
function tu($id,$model="2",$page=""){
    if($model=="2"){
        $act="news";
    }elseif($model=="0"){
        $act="category"; 
    }elseif($model=="3"){
        $act="chat";
    }
   if(is_numeric($id)){
     $mod_act_url=SITEURL.'/'.$act."".$id."".$page.".html";
   }else{
     $mod_act_url=SITEURL."/".$id."".$page.".html";
   }
    
   return $mod_act_url; 
}
function u($mod,$act='',$arr=array()){
	$wjt=0;
	if(isset($arr['rela'])){
		$rela=1;
		unset($arr['rela']);
	}
	else{
		$rela=0;
	}

	if(defined('INDEX')==1){
		if($act=='' && $mod=='index'){
			return SITEURL;
		}
	    global $wjt_mod_act_arr;  //伪静态数组
		
		if(!isset($wjt_mod_act_arr)){
			$wjt_mod_act_arr=ez_get_cache('wjt');
		}
	    if(WJT==1 && array_key_exists($mod,$wjt_mod_act_arr) && array_key_exists($act,$wjt_mod_act_arr[$mod]) && $wjt_mod_act_arr[$mod][$act]==1){
		    $wjt=1;
	    }
		unset($wjt_mod_act_arr);
		
		if($mod=='tao' && ($act=='list' || $act=='view') && URLENCRYPT!=''){
	        if(isset($arr['cid']) && $arr['cid']>0){
		        $arr['cid']=ez_encrypt($arr['cid'],URLENCRYPT);
		    }
		    elseif(isset($arr['iid']) && $arr['iid']>0){
		        $arr['iid']=ez_encrypt($arr['iid'],URLENCRYPT);
		    }
	    }
	}

	if($wjt==0){
		if($act==''){
	        $mod_act_url="index.php?mod=".$mod."&act=index";
	    }
	    elseif(empty($arr)){
	        $mod_act_url="index.php?mod=".$mod."&act=".$act;
	    }
	    else{
	        $mod_act_url="index.php?mod=".$mod."&act=".$act.arr2param($arr);
	    }
	}
	elseif($wjt==1){
		global $alias_mod_act_arr;  //链接别名数组
		if(!isset($alias_mod_act_arr)){
			$alias_mod_act_arr=ez_get_cache('alias');
		}
		$dir=$mod.'/'.$act;
		if(is_array($alias_mod_act_arr[$dir])){
		    $mod=$alias_mod_act_arr[$dir][0];
			$act=$alias_mod_act_arr[$dir][1];
		}
		unset($alias_mod_act_arr);
		if($act==''){
	        $mod_act_url=$mod."/index.html";
	    }
	    elseif(empty($arr)){
	        $mod_act_url=$mod.'/'.$act.'.html';
	    }
	    else{
			$mod_act_url='';
			$url='';
			foreach($arr as $k=>$v){
			    $url.=rawurlencode($v).'-';
			}
		    $mod_act_url=$mod.'/'.$act.'-'.$url;
		    $mod_act_url=str_del_last($mod_act_url).'.html';
	    }
	}
	
	if(defined('INDEX') && $mod=='index' && $act=='index'){
		$mod_act_url='';
	}
	
	if(defined('INDEX') && $rela==0){
		$mod_act_url=SITEURL.'/'.$mod_act_url;
	}

    return $mod_act_url;
}



function sel_date($dir){
    $dh = dir($dir);
    $j=0;
    while(($filename=$dh->read()) !== false){
	    if ($filename != "." && $filename != ".."){
			$dp=$dir.'/'.$filename;
			if(judge_empty_dir($dp)!=1){
			    $arr=explode('_',$filename);
	            $time=date('Y-m-d',strtotime($arr[1]));
	            $option_arr[$j]="<option value='$arr[1]'>$time</option>";
		        $j++;
			}
	    }
    }
    for($i=$j;$i>=0;$i--){
        $option.=$option_arr[$i];
    }
    $dh->close();
	return $option;
}

function mingxi_content($row,$mingxi_content){
	$mingxi_content=str_replace('{money}',$row['money'],$mingxi_content);
	$mingxi_content=str_replace('{jifenbao}',jfb_data_type($row['jifenbao']),$mingxi_content);
	$mingxi_content=str_replace('{jifen}',$row['jifen'],$mingxi_content);
    if(strpos($mingxi_content,'{source}')!==false){
	    $mingxi_content=str_replace('{source}',$row['source'],$mingxi_content);
	}
	return $mingxi_content;
}

function error_html($error_msg='缺少必要参数',$goto="",$type=0){
	global $nav;
	global $ezhand;
	global $webset;
	global $dduser;
	global $no_words;
	global $mallapiopen;
    include(ADMINROOT.'/template/error.tpl.php');
	ez_exit();
}

function spider_limit($spider) {
	foreach ($spider as $k=>$val) {
		if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), $k) !== false) {
			$rand_num = rand(1, 100);
			if ($rand_num <= $val) {
				ez_file_put(EZROOT . '/data/spider/' . $k . '.txt', date('Y-m-d H:i:s') . "\r\n", FILE_APPEND);
				error_html('hello spider!');
			}
		}
	}
}

function mod_name($mod,$act){
	if($mod=='index'){
	    $mod_name=$mod;
	}
	elseif($mod=='ajax' || $mod=='jump'){
	    $mod_name=$mod;
	}
    else{
	    $mod_name=$mod.'/'.$act;
	}
	return $mod_name;
}

function AD($id){
	$arr=ez_get_cache('ad/'.$id);
	if(!empty($arr)){
		$style='style="text-align:center;margin: auto;';
		if($arr['edate']>TIME && ($arr['img']==1 || $arr['content']==1)){
			if($arr['width']>0){
				$style.='width:'.$arr['width'].'px;';
			}
			if($arr['height']>0){
				$style.='height:'.$arr['height'].'px;';
			}
			$style.='"';
			if(isset($arr['ad_content'])){
				$c=$arr['ad_content'];
			}
			else{
				$c="<script src='".SITEURL."/data/ad/".$id.".js'></script>";
			}
			return "<div ".$style." id='ad".$id."'>".$c."</div>";
		}
	}
	return;
}

function yzm($path=''){
    return '<img alt="验证码" src="'.SITEURL.'/comm/showpic.php" align="absmiddle" onClick="this.src=\''.SITEURL.'/comm/showpic.php?a=\'+Math.random()" title="点击更换" style="cursor:pointer;"/>';
}



function reg_content($content,$type=0){ //type为1，替换；type为2，提示错误
	$pattern=DOMAIN_PREG;
	if($type==0){
		$type=REPLACE;
	}
    $shield_arr = ez_get_cache('no_words'); //屏蔽词语
	if($type==1){
		$content=strtr($content,$shield_arr);
		$content=preg_replace($pattern,'',$content);
	}
	else{
		foreach($shield_arr as $v){
			if(strpos($content,$v)!==false){
				return ''; //包含非法词汇
	    	}
		} 
		if(preg_match($pattern,$content)){
	    	return '';
		}
	}
	return htmlspecialchars($content);
}

function show_mobile($mobile){
	return '<b style="font-size:18px; color:#000">'.substr($mobile,0,3).'*****'.substr($mobile,-3).'</b>';
}

function ez_xuliehua($obj) { 
   return base64_encode(gzcompress(json_encode($obj))); 
} 

//反序列化
function ez_unxuliehua($txt) { 
   return json_decode(gzuncompress(base64_decode($txt)),1); 
}

function aez_menu($data){ //$data=array('parent_id'=>72,'node'=>'plug','mod'=>'plugin','act'=>'list','listorder'=>'0','sort'=>'0','title'=>'插件列表','hide'=>0,'sys'=>1);
	global $ezhand;
	
	if(!isset($data['parent_id'])){ //插件菜单快速添加
		$data['parent_id']=72;
		$data['node']='plug';
		$data['listorder']=0;
		$data['sort']=0;
		$data['hide']=1;
		$data['sys']=0;
	}
	
	if($data['act']=='' && $data['mod']==''){
		$data['listorder']=$data['sort']+10000;
		unset($data['sort']);
		$menuid=$ezhand->select('menu','id','`node`="'.$data['node'].'" and `mod`="" and `act`=""');
		if($menuid>0){
	   		return $menuid; //节点已存在;
		}
		$menuid=$ezhand->insert('menu',$data);
		$data=array('role_id'=>1,'menu_id'=>$menuid);
		$ezhand->insert('menu_access',$data);
		return $menuid;
	}
	else{
		$menuid=$ezhand->select('menu','id','`mod`="'.$data['mod'].'" and act="'.$data['act'].'"');
		if($menuid>0){
			return $menuid;
		}
		$menuid=$ezhand->insert('menu',$data);
		$data=array('role_id'=>1,'menu_id'=>$menuid);
		$ezhand->insert('menu_access',$data);
	}
}

function del_menu($mod,$act){
	global $ezhand;
	$id=$ezhand->select('menu','id','`mod`="'.$mod.'" and `act`="'.$act.'"');  //删除导航
	$ezhand->delete('menu','id="'.$id.'" limit 1');
	$ezhand->delete('menu_access','menu_id="'.$id.'" limit 1');
}

function show_ad($adpos){
	global $ezhand;
    $adinfo=$ezhand->select('adv a,advtype t','a.*,t.width,t.height','a.spaceid=t.id and a.spaceid='.$adpos);
    if(!empty($adinfo)){
        if(!empty($adinfo["adcode"])){
            return $adinfo["adcode"];
        }else{
            //if($adinfo["type"]==""){
            if(strpos($adinfo["picurl"],"ttp://")>0){
                
            }else{
                
                $adinfo["picurl"]=SITEURL."/".$adinfo["picurl"];
            }               
                
            $html="<a href=\"".$adinfo["url"]."\" target=\"_blank\"><img src=\"".$adinfo["picurl"]."\" width=\"".$adinfo["width"]."\" height=\"".$adinfo["height"]."\"></a>";
            //}
            $html='<table width="'.$adinfo["width"].'" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td class="banner'.$adinfo["width"].'">'.$html.'</td></tr></table>';
            
            return $html;
        }
        
    }else{
        return "";
    }
    
}



function url_html_cache($name,$url,$trigger_time_arr=array()){
	$trigger_time_arr=array('09:30:00','14:30:00','17:30:00');
	$html_dir=EZROOT.'/data/html/'.$name.'/'.ez_crc32($url).'.html';
	$html_url=SITEURL.'/data/html/'.$name.'/'.ez_crc32($url).'.html';
	
	if(!file_exists($html_dir)){
		$html=ez_get($url);
		create_file($html_dir,$html);
	}
	else{
		$file_time=filemtime($html_dir);
		foreach($trigger_time_arr as $v){
			$trigger_time=strtotime(date('Ymd'.' '.$v));
			if(TIME>$trigger_time && $file_time<=$trigger_time){
				$html=ez_get($url);
				create_file($html_dir,$html);
			}
		}
	}
	return $html_url;
}

function l($mod,$act,$arr=array()){
	$url=SITEURL.'/index.php?mod='.$mod.'&act='.$act;
	if(!empty($arr)){
		$url.='&'.arr2param($arr);
	}
	return $url;
}

function p($mod,$act,$arr=array()){
	$url='';
	if(WJT==1){
		foreach($arr as $k=>$v){
			$url.=rawurlencode($v).'-';
		}
		$url=$mod.'/'.$act.'-'.$url;
		$url=SITEURL.'/'.str_del_last($url).'.html';
	}
	else{
		$url=SITEURL.'/plugin.php?mod='.$mod.'&act='.$act;
		if(!empty($arr)){
			$url.=arr2param($arr);
		}	
	}
	return $url;
}

function include_mod($mod,$ezhand,$new=1){ //new表示是否实例化
	include(EZROOT.'/mod/'.$mod.'/fun.class.php');
	$ez_mod_class_name='ez_'.$mod.'_class';
	$$ez_mod_class_name=new $ez_mod_class_name($ezhand);
	return $$ez_mod_class_name;
}
?>